/**
 * event 定义了一批需要被执行成功多少次后才能结束的函数
 * @date 2020-08-22
 * @param {function} Func 需要执行的函数
 * @param {array} args 传入的参数数组
 * @param {number} times=1 需要执行的次数
 * @returns {number} 返回定时器的序号
 */
function event(Func, args, times = 1) {
    let { IntervalTime = 200, maxCount = 50, TimeLoop } = this;

    let index = new Date().getTime() + "" + Math.floor(Math.random() * 1000);
    let func = setInterval(async () => {
        TimeLoop[index].counter -= 1;
        try {
            await Func(...args);
            TimeLoop[index].needDoneTimes -= 1;
        } catch (err) {}
        if (!TimeLoop[index].counter || !TimeLoop[index].needDoneTimes) {
            clearInterval(TimeLoop[index].Interval);
            delete TimeLoop[index];
            console.log("事件完成");
        }
    }, IntervalTime);
    TimeLoop[index] = {
        needDoneTimes: times,
        counter: maxCount,
        //判断是否到达
        Interval: func,
    };
    return TimeLoop[index];
}
module.exports = event;
